<template>
    <transition name="fade">
        <div class="modal-bg" v-if="visible">
            <div class="editor-modal">
                <div class="upload-image">
                    <span class="editor-modal-close" @click="closeModal()">X</span>
                    <div class="editor-modal-title">插入图片</div>
                    <div class="upload-image-type">
                        <a :class="{ active: imageUpType === 1 }" @click="uploadType(1)">网络图片</a>
                        <a :class="{ active: imageUpType === 2 }" @click="uploadType(2)">本地上传</a>
                    </div>
                    <div class="upload-image-body" v-if="imageUpType === 1">
                        <div class="upload-input">
                            <label>网络图片URL：</label>
                            <textarea v-model="image_url" class="image_url"></textarea>
                        </div>
                        <div class="upload-button">
                            <a class="cancel" @click="closeModal()">取消</a>
                            <a class="submit" @click="confirm">确认</a>
                        </div>
                    </div>
                    <div class="upload-image-body" v-else>
                        <div class="upload-image-btn" @click="upload()">
                            <span>+</span>
                            <p>点击上传</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </transition>
</template>
<style lang="less" scoped>
.modal-bg {
    width: 100%;
    height: 100%;
    position: fixed;
    left: 0;
    top: 0;
    background: rgba(0, 0, 0, 0.05);
	z-index: 999;
}
.editor-modal {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 400px;
    height: 280px;
    z-index: 99;
    background: #fff;
    transform: translate(-50%, -50%);
    box-shadow: 0px 0px 3px rgba(0, 0, 0, 0.2);
    &.fade-enter-active,
    &.fade-leave-active {
        transition: all 0.3s cubic-bezier(0.68, 0, 0.27, 1.55);
    }
    &.fade-enter-from,
    &.fade-leave-to {
        transform: translate(-50%, -50%) scale(0.1, 0.1);
        opacity: 0;
    }
}
.upload-image {
    padding: 20px;
    .editor-modal-close {
        position: absolute;
        cursor: pointer;
        height: 20px;
        width: 20px;
        text-align: center;
        display: block;
        color: #888;
        font-size: 14px;
        right: 0;
        top: 0;
        &:hover {
            color: #000;
        }
    }
    .editor-modal-title {
        margin-top: -10px;
        font-size: 16px;
        margin-bottom: 15px;
    }
    .upload-image-type {
        border-bottom: 1px solid #ccc;
        font-size: 14px;
        line-height: 30px;
        padding-left: 10px;
        a {
            color: #888;
            margin-right: 15px;
            &.active,
            &:hover {
                color: #000;
                font-weight: bold;
            }
        }
    }
    .upload-input {
        margin-top: 20px;
        label {
            display: block;
            margin-bottom: 10px;
        }
        textarea {
            display: block;
            width: 100%;
            height: 56px;
            padding: 3px 5px;
            resize: none;
            border-color: #ccc;
            border-radius: 5px;
            transition: all 0.2s;
            box-shadow: 0px 0px 0px rgba(71, 135, 255, 0.2);
            outline: none;
            &:focus {
                border-color: #4787ff;
                box-shadow: 0px 0px 3px rgba(71, 135, 255, 0.2);
            }
        }
    }
    .upload-button {
        height: 80px;
        display: flex;
        justify-content: center;
        align-items: center;
        a {
            display: block;
            width: 80px;
            height: 30px;
            margin: 0 10px;
            text-align: center;
            line-height: 30px;
            border-radius: 3px;
            transition: all 0.2s;
            &.submit {
                background: #4787ff;
                color: #fff;
                &:hover {
                    background: #3676ee;
                }
            }
            &.cancel {
                color: #666;
                background: #eee;
                &:hover {
                    color: #000;
                    background: #ccc;
                }
            }
        }
    }
    .upload-image-btn {
        width: 80px;
        height: 80px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        margin: 50px auto;
        background: #eee;
        border-radius: 6px;
        text-align: center;
        color: #aaa;
        cursor: pointer;
        border: 1px solid #eee;
        transition: all 0.2s;
        span {
            display: block;
            font-size: 20px;
            line-height: 1;
            margin-top: -3px;
        }
        p {
            margin: 0;
            line-height: 1;
            margin-top: 5px;
        }
        &:hover {
            color: #4787ff;
            border-color: #4787ff;
            background: #fff;
        }
    }
}
</style>
<script>
import Upload from "@/util/upload";
export default {
    name: "upload-modal",
    data() {
        return {
            imageUpType: 1,
            image_url: "",
        };
    },
    props: {
        visible: {
            type: Boolean,
            default: false,
        },
    },
    mounted() {},
    watch: {
        visible(val) {
            console.log("watch:", val);
        },
    },
    methods: {
        uploadType(i) {
            this.imageUpType = i;
        },
        closeModal() {
            this.$emit("update:visible", false);
            this.image_url = "";
        },
        confirm() {
            if (this.image_url) {
                this.$emit("imageChange", this.image_url);
                this.$emit("update:visible", false);
                this.image_url = "";
            }
        },
        upload() {
            Upload.Single({ fileType: [".png", ".jpg", ".jpeg", ".gif"] }).then((res) => {
                if (res.code === 0) {
                    this.$emit("imageChange", res.data.fileUrl);
                    this.$emit("update:visible", false);
                    this.image_url = "";
                }
            });
        },
    },
};
</script>
